"use strict";
var AngleCom_1 = require('./AngleCom');
var q = require('q');
var co = require('co');
var BaseController_1 = require('./../BaseController');
/**
 * 控制倾角计的基础类,不同的测量控制器应继承自该类
 */
class AngleController extends BaseController_1.BaseController {
    /**
     * 初始化控制器,打开com口
     */
    constructor(portName) {
        super();
        /**
         * 控制倾角计的anglecom
         */
        this.angleCom = null;
        /**
         * 倾角计测量点的集合
         */
        this.deviceList = [];
        this.comPort = this.angleCom = new AngleCom_1.AngleCom(portName);
    }
    /**
     * 倾角计测量,返回一组promise的测点数据
     * 构建一个AnglePoint的集合数据,测量完成后,将测量结果填入每个AnglePoint中,最终返回该AnglePoint的集合
     */
    survey() {
        //测量前的准备工作
        this.preSurvey && this.preSurvey();
        var resultPoints = []; //准备好本次的测量点
        var _this = this;
        return co(function* () {
            if (_this._working)
                return null;
            _this._working = true;
            yield q.delay(800); //略微延迟一下,防止串口未打开
            try {
                for (let point of _this.deviceList) {
                    //测量该点
                    var result = yield _this.angleCom.survey(point.id);
                    var pt = Object.assign(new AnglePoint(0, 0), point);
                    //如果能观测到数据
                    if (result && (result.x !== null || result.y !== null)) {
                        //赋值到该点的角度和距离
                        _this.parseResult(pt, result);
                    }
                    else {
                        yield q.delay(100);
                    }
                    resultPoints.push(pt);
                }
                _this._working = false;
                return resultPoints;
            }
            catch (error) {
                _this._working = false;
                console.log('Error' + error);
                return resultPoints;
            }
        });
    }
    /**
     * 根据倾角计返回原始数据，得到该点测量结果
     */
    parseResult(point, result) {
        switch (point.angleType) {
            case 1:
                point.x = result.x * point.resolution;
                point.t = result.t;
                point.time = new Date();
                break;
            case 2:
                point.y = result.y * point.resolution;
                point.t = result.t;
                point.time = new Date();
                break;
            default:
                point.x = result.x * point.resolution;
                point.y = result.y * point.resolution;
                point.t = result.t;
                point.time = new Date();
                break;
        }
    }
}
exports.AngleController = AngleController;
/**
 * 倾角计测量点
 */
class AnglePoint {
    /**
    * 构造函数
    * @param {number} id 测量点编号 re 分辨率
    */
    constructor(id, re) {
        this.id = 1;
        this.x = null;
        this.y = null;
        this.t = null;
        this.angleType = 0;
        this.resolution = 0.001;
        this.time = null; // 测量时间
        this.id = id || 1;
        re && (this.resolution = re);
    }
}
exports.AnglePoint = AnglePoint;
/**
 * 不同倾角计返回的有效数据类型不同
 * 单轴倾角计返回结果中只有X轴或Y轴数据是有效的；双轴倾角计返回的X、Y数据都有效
 */
var ResultType;
(function (ResultType) {
    ResultType[ResultType["XYAngle"] = 0] = "XYAngle";
    ResultType[ResultType["XAngle"] = 1] = "XAngle";
    ResultType[ResultType["YAngle"] = 2] = "YAngle"; // 倾角计Y轴数据    
})(ResultType || (ResultType = {}));
exports.ResultType = ResultType;
